package com.xxl.job.executor.util;


import org.apache.commons.codec.digest.DigestUtils;

import java.io.UnsupportedEncodingException;

/**
  *MD5加密组件,如需不够用，扩展DigestUtils方法
  * @date 2019-10-16
  * @auther: linmac
  */
public  class MD5 {
	/**
	 * MD5加密
	 * 
	 * @param data
	 *            待加密数据
	 * @return byte[] 消息摘要
	 * 
	 * @throws Exception
	 */
	public static byte[] encodeMD5(String data)  {
		try{
		// 执行消息摘要
		return DigestUtils.md5(data);
		}catch ( Exception e){
			throw  new RuntimeException(e);
		}
	}

	/**
	 * MD5加密
	 * 
	 * @param data
	 *            待加密数据
	 * @return String 消息摘要
	 * 
	 * @throws Exception
	 */
	public static String encodeMD5Hex(String data, String charset) {
		// 执行消息摘要
		return DigestUtils.md5Hex(getContentBytes(data,charset)).toUpperCase();
	}

	/**
	 * MD5加密
	 *
	 * @param data
	 *            待加密数据
	 * @return byte[] 消息摘要
	 *
	 * @throws Exception
	 */
	public static String encodeMD5Hex(byte[] data) {
		// 执行消息摘要
		return DigestUtils.md5Hex(data).toUpperCase();
	}



	/**
	 * @param content
	 * @param charset
	 * @return
	 *
	 * @throws UnsupportedEncodingException
	 */
	private static byte[] getContentBytes(String content, String charset) {
		if (charset == null || "".equals(charset)) {
			return content.getBytes();
		}
		try {
			return content.getBytes(charset);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
		}
	}

	public static void main(String[] args) throws Exception {
	/*	String a="appid=wx0eed0443e779b781&attach=232424&auth_code=3241414324242444&body=E-Serve&device_info=100002&mch_id=1232599101&nonce_str=pe3jxysoeftyk9qlr584drbs14ls6nxx&out_trade_no=1470122807901&time_start=20160802152647&total_fee=1&key=a55662097d424814a457d90476c7a557";
		try {
			ReqProMtcPathEntity reqProMtcPathEntity = new ReqProMtcPathEntity();
			BoundaryPoint boundaryPoint = new BoundaryPoint();
			boundaryPoint.setProvinceId("44");
			boundaryPoint.setMediaType(2);
			boundaryPoint.setMediaNo("441324333333324");
			boundaryPoint.setVehicleId("京A12345_1");
			boundaryPoint.setVehicleType(26);
			boundaryPoint.setVehicleClass(22);
			boundaryPoint.setRoadNodeIdArr("G0040121|G0040123|G0040109");
			boundaryPoint.setPassTimeArr("20191016101010|20191016101610|20191016101910");
			boundaryPoint.setEnTollLaneId("1");
			boundaryPoint.setEnTime("2019-10-18T10:18:18");
			boundaryPoint.setExTime("2019-10-18T10:18:18");
			//boundaryPoint.setPassId("132333333333333320190926091325");
			boundaryPoint.setROWKEY("ADNEADEAWH");
			SimplePropertyPreFilter filter = new SimplePropertyPreFilter(BoundaryPoint.class, "ROWKEY");
			PropertyFilter profilter = new PropertyFilter(){

				@Override
				public boolean apply(Object object, String name, Object value) {
					if(name.equalsIgnoreCase("ROWKEY")){
						//false表示last字段将被排除在外
						return false;
					}
					return true;
				}

			};
			String json = JSONObject.toJSONString(boundaryPoint,profilter);
			System.out.println(json);
			byte[] bytes =GZipUtils.compress(json.getBytes());
			String fileMd5 = MD5.encodeMD5Hex(bytes);//转json MD5摘要

			reqProMtcPathEntity.setBinFile(bytes);
			reqProMtcPathEntity.setBinfile_md5(fileMd5);
			reqProMtcPathEntity.setBinfile_gzip("true");
			reqProMtcPathEntity.setFilename("FEECALC_PRT_REQ_36_20160415205958123.zip");

			HttpRequestConfig config = HttpRequestConfig.create().url("http://10.173.232.101:8199/api/v1/fee")
					.httpEntityType(HttpEntityType.ENTITY_MULTIPART);
			*//*HttpRequestConfig config = HttpRequestConfig.create().url("http://10.173.204.7:8085/freeflow-data-api/v1/api/fee/feeCalc_test")
					.httpEntityType(HttpEntityType.ENTITY_MULTIPART);*//*
			config.addHeader("binfile-auth",reqProMtcPathEntity.getBinfile_auth());
			config.addHeader("binfile-gzip",reqProMtcPathEntity.getBinfile_gzip());
			config.addHeader("binfile-md5",reqProMtcPathEntity.getBinfile_md5());
			config.bytes(bytes);
			config.setFileKey("binFile");
			config.setFileName(reqProMtcPathEntity.getFilename());
			config.addParam("filename",reqProMtcPathEntity.getFilename());
			//config.addParam("binFile",reqProMtcPathEntity.getFilename());
			HttpRequestResult result = HttpClientUtils.postUploadFile(config);
			//JSONObject j = JSONObject.parseObject(result.getResponseText());
			System.out.println(result.getResponseText());
		} catch (BSException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			throw  new BSException ("MD5待加密实体类bean转map出错",e);
		}*/
	}

}
